﻿using System;
using System.Text;

using LeanEngine.Extensions;
using LeanEngine.Exception;

namespace LeanEngine.Validation
{
    /// <summary>
    /// Attribute for validate whether the value is less than the comparison value.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public sealed class IsLessThanAttribute : ValidationAttribute
    {
        #region Constructor Members

        #region String Constructor Members

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a date or numeric comparison value 
        /// and an error message obtained from a resource file.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentException"></exception>
        /// <param name="resourceType">The System.Type of the resource manager that holds the error message.</param>
        /// <param name="errorKey">The key error message contained in the resource.</param>
        /// <param name="compareValue">An formatted string representing the a date or numeric value.</param>
        /// <param name="convertType">The type that the formatted string represents.</param>
        /// <remarks>
        ///     For dates you must use an ISO8601 formatted string like "2006-01-20T00:00:00".
        ///     For decimal values you must use an english formatted value like "1.99".
        /// </remarks>
        public IsLessThanAttribute(string compareValue, ArgumentConvertType convertType, Type resourceType, string errorKey)
            : base(resourceType, errorKey)
        {
            this.CompareValue = ConvertArgument(compareValue, convertType);
        }

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a date or numeric lower and an error message.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentException"></exception>
        /// <param name="compareValue">An formatted string representing the a date or numeric value.</param>
        /// <param name="convertType">The type that the formatted string represents.</param>
        /// <param name="errorMessage">The error message displayed for non valid values.</param>
        /// <remarks>
        ///     For dates you must use an ISO8601 formatted string like "2006-01-20T00:00:00".
        ///     For decimal values you must use an english formatted value like "1.99".
        /// </remarks>
        public IsLessThanAttribute(string compareValue, ArgumentConvertType convertType, string errorMessage)
            : base(errorMessage)
        {
            this.CompareValue = ConvertArgument(compareValue, convertType);
        }

        #endregion

        #region Byte Constructor Members

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a comparison value 
        /// and an error message obtained from a resource file.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="compareValue">A value representing the lower bound.</param>
        /// <param name="resourceType">The System.Type of the resource manager that holds the error message.</param>
        /// <param name="errorKey">The key error message contained in the resource.</param>
        public IsLessThanAttribute(Byte compareValue, Type resourceType, string errorKey)
            : base(resourceType, errorKey)
        {
            CompareValue = compareValue;
        }

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a comparison value and an error message.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="compareValue">A value representing the lower bound.</param>
        /// <param name="errorMessage">The error message displayed for non valid values.</param>
        public IsLessThanAttribute(Byte compareValue, string errorMessage)
            : base(errorMessage)
        {
            CompareValue = compareValue;
        }

        #endregion

        #region Int16 Constructor Members

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a comparison value  
        /// and an error message obtained from a resource file.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="compareValue">A value representing the lower bound.</param>
        /// <param name="resourceType">The System.Type of the resource manager that holds the error message.</param>
        /// <param name="errorKey">The key error message contained in the resource.</param>
        public IsLessThanAttribute(Int16 compareValue, Type resourceType, string errorKey)
            : base(resourceType, errorKey)
        {
            CompareValue = compareValue;
        }

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a comparison value and an error message.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="compareValue">A value representing the lower bound.</param>
        /// <param name="errorMessage">The error message displayed for non valid values.</param>
        public IsLessThanAttribute(Int16 compareValue, string errorMessage)
            : base(errorMessage)
        {
            CompareValue = compareValue;
        }

        #endregion

        #region Int32 Constructor Members

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a comparison value  
        /// and upper bound and an error message obtained from a resource.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="compareValue">A value representing the lower bound.</param>
        /// <param name="resourceType">The System.Type of the resource manager that holds the error message.</param>
        /// <param name="errorKey">The key error message contained in the resource.</param>
        public IsLessThanAttribute(Int32 compareValue, Type resourceType, string errorKey)
            : base(resourceType, errorKey)
        {
            CompareValue = compareValue;
        }

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a comparison value and an error message.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="compareValue">A value representing the lower bound.</param>
        /// <param name="errorMessage">The error message displayed for non valid values.</param>
        public IsLessThanAttribute(Int32 compareValue, string errorMessage)
            : base(errorMessage)
        {
            CompareValue = compareValue;
        }

        #endregion

        #region Int64 Constructor Members

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a comparison value  
        /// and an error message obtained from a resource file.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="compareValue">A value representing the lower bound.</param>
        /// <param name="resourceType">The System.Type of the resource manager that holds the error message.</param>
        /// <param name="errorKey">The key error message contained in the resource.</param>
        public IsLessThanAttribute(Int64 compareValue, Type resourceType, string errorKey)
            : base(resourceType, errorKey)
        {
            CompareValue = compareValue;
        }

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a comparison value and an error message.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="compareValue">A value representing the lower bound.</param>
        /// <param name="errorMessage">The error message displayed for non valid values.</param>
        public IsLessThanAttribute(Int64 compareValue, string errorMessage)
            : base(errorMessage)
        {
            CompareValue = compareValue;
        }

        #endregion

        #region Decimal Constructor Members

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a comparison value  
        /// and an error message obtained from a resource file.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="compareValue">A value representing the lower bound.</param>
        /// <param name="resourceType">The System.Type of the resource manager that holds the error message.</param>
        /// <param name="errorKey">The key error message contained in the resource.</param>
        public IsLessThanAttribute(Decimal compareValue, Type resourceType, string errorKey)
            : base(resourceType, errorKey)
        {
            CompareValue = compareValue;
        }

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a comparison value and an error message.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="compareValue">A value representing the lower bound.</param>
        /// <param name="errorMessage">The error message displayed for non valid values.</param>
        public IsLessThanAttribute(Decimal compareValue, string errorMessage)
            : base(errorMessage)
        {
            CompareValue = compareValue;
        }

        #endregion

        #region Datetime Constructor Members

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a comparison value  
        /// and upper bound and an error message obtained from a resource.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="compareValue">A value representing the lower bound.</param>
        /// <param name="resourceType">The System.Type of the resource manager that holds the error message.</param>
        /// <param name="errorKey">The key error message contained in the resource.</param>
        public IsLessThanAttribute(DateTime compareValue, Type resourceType, string errorKey)
            : base(resourceType, errorKey)
        {
            CompareValue = compareValue;
        }

        /// <summary>
        /// Initialize a new instance of LeanEngine.Validation.IsLessThanAttribute class with a comparison value and an error message.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <param name="compareValue">A value representing the lower bound.</param>
        /// <param name="errorMessage">The error message displayed for non valid values.</param>
        public IsLessThanAttribute(DateTime compareValue, string errorMessage)
            : base(errorMessage)
        {
            CompareValue = compareValue;
        }

        #endregion        

        #endregion

        #region PropertyInfo Members

        /// <summary>
        /// The comparison value that is expected to be greater.
        /// </summary>
        public object CompareValue
        {
            get;
            private set;
        }

        #endregion

        #region ValidationAttribute Members

        /// <summary>
        /// Validates the value according to the validation rule and throws an exception in case of validation failure.
        /// </summary>
        /// <exception cref="LeanEngine.Exception.BusinessException"></exception>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        /// <param name="value">The value to be validated.</param>
        public override bool Validate(object value)
        {
            if (value.IsNull() || !value.IsLessThan(this.CompareValue))
            {
                throw new BusinessException(this.ErrorMessage);
            }

            return true;
        }

        /// <summary>
        /// Validates the value according to the validation rule to the container and adds exception error in case of validation failure.
        /// </summary>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        /// <param name="value">The value to be validated.</param>
        /// <param name="errorMessageContainer">The container for all the error messages.</param>
        public override void Validate(object value, StringBuilder errorMessageContainer)
        {
            if (value.IsNull() || !value.IsLessThan(this.CompareValue))
            {
                errorMessageContainer.Append(this.ErrorMessage);
                errorMessageContainer.Append(Environment.NewLine);
            }
        }

        #endregion
    }
}
